package entity;

import java.util.List;
import java.util.Optional;

public class Person {

  private String masterNumber;
  private Address address;

  private List<Telephone> telephones;

  private List<Email> emails;

  public Person() {
  }

  public Person(String masterNumber, List<Telephone> telephones, Address address,
                List<Email> emails) {
    this.masterNumber = masterNumber;
    this.address = address;
    this.telephones = telephones;
    this.emails = emails;
  }

  public Optional<SimpleAddress> getSimpleAddress() {
    //TODO: return Optional<SimpleAddress>
    if(Helper.isNullOrEmpty(address)){
      return Optional.empty();
    } else {
      SimpleAddress simpleAddress = new SimpleAddress(address.getStreet(), address.getCity());
      return Optional.of(simpleAddress);
    }
  }

  public Address getAddress() {
    return address;
  }

  public List<Email> getEmails() {
    return emails;
  }

  public List<Telephone> getTelephones() {
    return telephones;
  }

  public static String giveTelephones(List<Telephone> telephones){
    String info = "";
    for(Telephone telephone: telephones){
      info += telephone.returnTelephone()+" ";
    }
    return info;
  }

  public static String giveEmails(List<Email> emails){
    String info = "";
    for(Email email: emails){
      info += email.getEmailAddress()+" ";
    }
    return info;
  }

  @Override
  public String toString() {
    String info = String.format("This is Person %s, address: %s, telephone: %s, email: %s.",
            masterNumber, address.returnAddress(), giveTelephones(telephones), giveEmails(emails));
    return info;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Person other = (Person) obj;
    if (masterNumber == null) {
      if (other.masterNumber != null)
        return false;
    } else if (!masterNumber.equals(other.masterNumber))
      return false;
    if (address == null) {
      if (other.address != null)
        return false;
    } else if (!address.equals(other.address))
      return false;
    if (emails == null) {
      if (other.emails != null)
        return false;
    } else if (!emails.equals(other.emails))
      return false;
    if (telephones == null) {
      if (other.telephones != null)
        return false;
    } else if (!telephones.equals(other.telephones))
      return false;
    return true;
  }
}
